package com.pb.poem.plantform.qq.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.pb.poem.entity.User;
import com.pb.poem.plantform.API;
import com.pb.poem.plantform.qq.entity.QQUser;
import com.pb.poem.util.ConfigManager;
import com.pb.poem.util.Constants;
import com.qq.open.OpenApiV3;
import com.qq.open.OpensnsException;

public class QQAPI implements API{
	
	

  
    
    // 指定OpenApi Cgi名字 
    private String getInfo = "/v3/user/get_info";
    private String getAppFirend = "/v3/relation/get_app_friends";

    // 指定HTTP请求协议类型
    String http = "http";
    String https = "https";
    
    private String openid;
    
    private String openkey;
    
    private String protocol;
    // 所要访问的平台, pf的其他取值参考wiki文档: http://wiki.open.qq.com/wiki/API3.0%E6%96%87%E6%A1%A3
    private String pf = "qzone";
    

    private  OpenApiV3 sdk;
    
    public QQAPI(){
    	 sdk = new OpenApiV3(Constants.appid, Constants.appkey);
    	 sdk.setServerName(ConfigManager.getInstance().getString("qq.server.formal"));
    	 protocol = http;
    	 pf = "qzone";
    }
	
    

	@Override
	public User getUser(String openid1, String openkey1) {
		try
        {
        	 // 填充URL请求参数
            HashMap<String,String> params = new HashMap<String, String>();
            params.put("openid", this.openid);
            params.put("openkey", this.openkey);
            params.put("pf", this.pf);
            JSONObject resp = sdk.api(getInfo, params, this.protocol);
            int ret = resp.getInt("ret");
            if(ret!=0){
            	throw new OpensnsException(ret,resp.getString("msg"));
            }
            QQUser user = new QQUser();
            user.setRet(resp.getInt("ret"));
            user.setMsg(resp.optString("msg"));
            user.setUserid(params.get("openid"));
            user.setNickname(resp.optString("nickname"));
            user.setFigureurl(resp.optString("figureurl"));
            user.setCity(resp.optString("city"));
            user.setProvince(resp.optString("province"));
            user.setCountry(resp.optString("country"));
            user.setIs_yellow_vip(resp.getInt("is_yellow_vip"));
            System.out.println(resp);
            return user;
        }
        catch (OpensnsException e)
        {
            System.out.printf("Request Failed. code:%d, msg:%s\n", e.getErrorCode(), e.getMessage());
        } catch (JSONException e) {
			e.printStackTrace();
		}
        return null;
	}

	@Override
	public List<String> getFriendIds() {
		List<String> list = new ArrayList<String>();
		try
        {
        	 // 填充URL请求参数
            HashMap<String,String> params = new HashMap<String, String>();
            params.put("openid", openid);
            params.put("openkey", openkey);
            params.put("pf", pf);
            JSONObject resp = sdk.api(getAppFirend, params, http);
            int ret = resp.getInt("ret");
            if(ret!=0){
            	throw new OpensnsException(ret,resp.getString("msg"));
            }
            
            JSONArray ja = resp.getJSONArray("items");
            for (int i = 0;i<ja.length();i++) {
				list.add(ja.getJSONObject(i).getString("openid"));
			}
            
            return list;
        }
        catch (OpensnsException e)
        {
            System.out.printf("Request Failed. code:%d, msg:%s\n", e.getErrorCode(), e.getMessage());
        } catch (JSONException e) {
			e.printStackTrace();
		}
        return null;
	}



	public String getPf() {
		return pf;
	}



	public void setPf(String pf) {
		this.pf = pf;
	}



	public String getOpenid() {
		return openid;
	}



	public void setOpenid(String openid) {
		this.openid = openid;
	}



	public String getOpenkey() {
		return openkey;
	}



	public void setOpenkey(String openkey) {
		this.openkey = openkey;
	}



	public String getProtocol() {
		return protocol;
	}



	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}



	@Override
	public User getUser() {
		// TODO Auto-generated method stub
		return getUser(null,null);
	}

}
